package com.neo.web.beans;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    private int code;

    private String message;

    private T data;

    public static <T> Result<T> ok(T data) {
        return of(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> okWithoutData() {
        return ok(null);
    }

    public static <T> Result<T> of(ResultCode resultCode) {
        return of(resultCode, null);
    }

    public static <T> Result<T> of(ResultCode resultCode, T data) {
        return of(resultCode.getCode(), resultCode.getMessage(), data);
    }

    private static <T> Result<T> of(int code, String message, T data) {
        ResultBuilder<T> builder = Result.builder();
        builder.code(code).message(message);
        if (data != null) {
            builder.data(data);
        }
        return builder.build();
    }
}
